package com.own.core.html.panel.checkBox;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Set;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;

import com.own.core.html.component.DetachableDaoModel;
import com.own.core.html.form.checkBox.OwnAjaxCheckBox;
import com.own.core.html.panel.OwnPanel;
import com.own.core.object.Dao;
import com.own.core.operation.DbOperation;

public class OwnSimpleCheckBoxPanel extends OwnPanel {

	public OwnSimpleCheckBoxPanel(String id, final IModel model, Item item) {
		super(id, model);

		OwnAjaxCheckBox ownCheckBox = new OwnAjaxCheckBox("checkBoxField",
				model) {

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				// model;
				Object obj = model;
				getDefaultModelObject();
//				System.out.println("test");

			}

		};

		add(ownCheckBox);
	}

	Collection collection = null;
	DetachableDaoModel modelDao;
	Dao dao;

	public OwnSimpleCheckBoxPanel(String id, IModel model, final Dao dao,
			final Field field) {
		super(id, model);
		this.dao = dao;
		try {
			modelDao = (DetachableDaoModel) model;
			Object obj = getObject(dao, field);
			if (obj instanceof Collection) {

				collection = (Collection) obj;
			}

			OwnAjaxCheckBox ownCheckBox = new OwnAjaxCheckBox("checkBoxField",
					model) {

				@Override
				protected void onUpdate(AjaxRequestTarget target) {
					boolean isChecked = (Boolean) getDefaultModelObject();
					saveOperation(isChecked, dao, field);
				}

				@Override
				protected void onComponentTag(final ComponentTag tag) {

					super.onComponentTag(tag);
					if (isChecked()) {
						tag.put("checked", "checked");
					} else {
						// In case the attribute was added at design time
						tag.remove("checked");
					}
				}

			};

			// ownCheckBox.ModelObject(true);
			add(ownCheckBox);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	void saveOperation(boolean isChecked, Dao dao, Field field) {
		if (isChecked) {
			collection.add(modelDao.getDao());
		} else {
			collection.remove(modelDao.getDao());

		}
		DbOperation.getME().saveOrUpdate(dao);

	}

	boolean isChecked() {
		return collection.contains(modelDao.getDao());
	}

	Object getObject(Dao dao, Field field) throws Exception {
		String fieldName = field.getName().substring(0, 1).toUpperCase()
				+ field.getName().substring(1).toLowerCase();
		Method method = dao.getClass().getMethod("get" + fieldName, null);

		return method.invoke(dao, null);
	}

//	void setObject(Dao dao, Field field, Object obj) {
//		try {
//			String fieldName = field.getName().substring(0, 1).toUpperCase()
//					+ field.getName().substring(1).toLowerCase();
//			
//			Method method = dao.getClass().getMethod("set" + fieldName,field.getType());
//			Set  set=new java.util.HashSet();
//			set.addAll((Collection) obj);
//			method.invoke(dao, set);
//		} catch (Exception e) {
////			e.printStackTrace();;
//		 
//		}
//	}

}
